<style scoped>
.submit{padding: 10px 20px;}
</style>
<template>
  <HeaderSlot>
    <van-nav-bar :title="this.id>0?$t('地址编辑'):$t('添加地址')" left-arrow @click-left="href_handle(-1)" />
  </HeaderSlot>
  <div class="address_edit">
    <form_param ref="form" :config="config" :submit="submit">
      <template #submit>
        <div class="submit">
          <van-button style="width: 100%;" type="primary" text="提交" native-type="submit"/>
        </div>
      </template>
    </form_param>
  </div>
</template>

<script>

import form_param from '@/components/form_param.vue'
import {s94_log, decode_config} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";
import {_get, href_handle} from "@/vue_plugin/router_auto.js";
import HeaderSlot from "@/components/slot/HeaderSlot.vue";

export default {
  components: {HeaderSlot, form_param},

  data(){
    let id = _get("id", 0);
    return {
      id: id,
      config: {
        // address_text: {name:"", label:'智能填写', type:'text_more'},
        name: {name:"name", label:'姓名', type:'text_one', tips:''},
        phone: {name:"phone", label:'电话', type:'text_one', config: 'number'},
        area_id: {name:"area_id", label:'地区', type:'select_one', config:[], tips:'请选择地址', value:"2429"},
        default: {name:"default", label:'默认', type:'radio_switch',value:0},
        address: {name:"address", label:'详细地址', type:'text_one'},
      },
    }
  },
  methods:{
    href_handle,
    init(){
      if (this.id > 0) {
        ajax("/home/address/info?id="+this.id, ({data})=>{
          this.$refs.form.init(data);
          return false;
        })
      }
      ajax({url:"/home/address/area_map", cache:true}, ({data})=>{
        let config = decode_config(data, {
          content_key:'name',
          value_key:'id',
          index_key:'id',
          parent_key:'parent_id'
        })
        this.config.area_id.config = config;
      })
    },
    submit(form_data){
      return new Promise((resolve, reject)=>{
        let url = '/home/address/' + (this.id>0 ? 'edit?id='+this.id : 'add');
        ajax({url:url, data:form_data, loading:1, method:'POST'}, (res)=>{
          href_handle(-1, 500);
        }, (res)=>{ reject(res.errors); });
      })
    },
  },
  watch:{

  },
  computed:{},
  mounted(){
    this.init();

  }
}
</script>